package cn.didi.feidi.location.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.navi.enums.PathPlanningStrategy;
import com.amap.api.navi.model.AMapNaviPath;
import com.amap.api.navi.model.NaviLatLng;
import com.amap.api.navi.view.RouteOverLay;

import java.util.ArrayList;
import java.util.HashMap;

import cn.didi.feidi.location.R;
import cn.didi.feidi.location.activity.base.BaseRouteSelectMapActivity;

/**
 * Created by jinxiong on 2017/7/10.
 */

public class RouteSelectActivity extends BaseRouteSelectMapActivity {

    private static final String TAG = "RouteSelectActivity";

    /**
     * protected AMapNavi mAMapNavi;
     * protected NaviLatLng startLatLng;//开始导航的起点
     * protected NaviLatLng endLatLng;//结束导航的终点
     * protected SparseArray<RouteOverLay> currentOverLayout;//存放可选路径
     * protected ArrayList<Integer> keys;//存放可选路径的方案号
     */


    private Button selectRouteBtn;
    private int currentRouteIndex = -1;

    /**
     * @param activity
     * @param startLatLng 起点
     * @param endLatLng   终点
     */

    public static void startThisActivity(Activity activity, NaviLatLng startLatLng,
                                         NaviLatLng endLatLng) {

        Intent intent = new Intent(activity, RouteSelectActivity.class);
        intent.putExtra("startLatLng", startLatLng);
        intent.putExtra("endLatLng", endLatLng);
        activity.startActivity(intent);
    }

    @Override

    protected void initView(Bundle saveInstanceState) {
        setContentView(R.layout.act_location_select_route);
        mMapView = (MapView) findViewById(R.id.map_view);
        selectRouteBtn = (Button) findViewById(R.id.select_route_btn);

        selectRouteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (keys.size() == 0 || currentOverLayout.size() == 0) {
                    Toast.makeText(RouteSelectActivity.this, "无fuck 可说", Toast.LENGTH_SHORT).show();
                    return;
                } else if (keys.size() == 1 && currentOverLayout.size() == 1) {
                    Toast.makeText(RouteSelectActivity.this, "只有一种路线", Toast.LENGTH_SHORT).show();
                    mAMapNavi.selectRouteId(keys.get(0));
                } else {
                    selectRoute();
                }
            }
        });


    }

    private void selectRoute() {

        currentRouteIndex++;
        if (currentRouteIndex >= keys.size()) {
            currentRouteIndex = 0;
        }

        for (int i = 0; i < keys.size(); i++) {
            currentOverLayout.get(keys.get(i)).setTransparency(0.4f);
        }

        currentOverLayout.get(keys.get(currentRouteIndex)).setTransparency(1);
        mAMapNavi.selectRouteId(keys.get(currentRouteIndex));

    }

    @Override
    protected void initVariables() {
        Intent intent = getIntent();
        startLatLng = intent.getParcelableExtra("startLatLng");
        endLatLng = intent.getParcelableExtra("endLatLng");
        mAMap = mMapView.getMap();
    }

    @Override
    protected void loadData() {

        calculateRoutes();
    }

    @Override
    protected void addListener() {

    }

    private void calculateRoutes() {
        mAMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(startLatLng.getLatitude(), startLatLng.getLongitude()), 12));
        switch (mode) {

            case DRIVE: {
                ArrayList<NaviLatLng> startList = new ArrayList<>(1);
                startList.add(startLatLng);
                ArrayList<NaviLatLng> endList = new ArrayList<>(1);
                endList.add(endLatLng);

                mAMapNavi.calculateDriveRoute(startList, endList, new ArrayList<NaviLatLng>(),
                        PathPlanningStrategy.DRIVING_MULTIPLE_ROUTES_AVOID_CONGESTION);
                break;
            }
            case WALK: {
                mAMapNavi.calculateWalkRoute(startLatLng, endLatLng);
                break;
            }
            case RIDE: {
                mAMapNavi.calculateRideRoute(startLatLng, endLatLng);
                break;
            }


        }
    }

    @Override
    public View getInfoWindow(Marker marker) {

        ViewGroup group = (ViewGroup) getLayoutInflater().inflate(R.layout.info_window_location_select_route, null, false);


        Button driveBtn = (Button) group.findViewById(R.id.drive_btn);
        Button walkBtn = (Button) group.findViewById(R.id.walk_btn);
        Button rideBtn = (Button) group.findViewById(R.id.ride_btn);
        Button navigationBtn = (Button) group.findViewById(R.id.navigation_btn);

        driveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mode = DRIVE;
                calculateRoutes();
            }
        });

        walkBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mode = WALK;
                calculateRoutes();
            }
        });

        rideBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mode = RIDE;
                calculateRoutes();
            }
        });

        navigationBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (keys.size() <= 0) {
                    Toast.makeText(RouteSelectActivity.this, "出事了", Toast.LENGTH_SHORT).show();
                    return;
                } else if (keys.size() == 1) {
                    mAMapNavi.selectRouteId(keys.get(0));
                } else {
                    if (currentRouteIndex != -1) {
                        Log.d(TAG, "onClick: keys.get(currentRouteIndex)  " + keys.get(currentRouteIndex));
                        mAMapNavi.selectRouteId(currentOverLayout.keyAt(currentRouteIndex));
                    } else {
                        Toast.makeText(RouteSelectActivity.this, "先选择路线", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }

                NavigationActivity.startThisActivity(RouteSelectActivity.this);
            }
        });


        return group;
    }

    @Override
    public boolean onMarkerClick(Marker marker) {


        return false;
    }

    //单路
    @Override
    public void onCalculateRouteSuccess() {

        AMapNaviPath path = mAMapNavi.getNaviPath();
        if (path == null) return;

        // TODO: 2017/7/11 清除overlay 在地图上

        for (int i = 0; i < currentOverLayout.size(); i++) {
            RouteOverLay overlay = currentOverLayout.get(keys.get(i));
            overlay.removeFromMap();
            overlay.destroy();
        }
        keys.clear();
        currentOverLayout.clear();


        drawPath(-1, path);


    }

    //多路
    @Override
    public void onCalculateMultipleRoutesSuccess(int[] ints) {
        //清空上次计算的路径列表。
        // TODO: 2017/7/11 清除overlay 在地图上

        for (int i = 0; i < currentOverLayout.size(); i++) {
            RouteOverLay overlay = currentOverLayout.get(keys.get(i));
            overlay.removeFromMap();
            overlay.destroy();
        }

        currentOverLayout.clear();
        keys.clear();

        HashMap<Integer, AMapNaviPath> paths = mAMapNavi.getNaviPaths();
        for (int i = 0; i < ints.length; i++) {
            Log.d(TAG, "onCalculateMultipleRoutesSuccess: ints[i]" + ints[i]);

            AMapNaviPath path = paths.get(ints[i]);
            if (path != null) {
                Log.d(TAG, "onCalculateMultipleRoutesSuccess: getAllLength " + path.getAllLength());
                Log.d(TAG, "onCalculateMultipleRoutesSuccess: getTollCost " + path.getTollCost());
                Log.d(TAG, "onCalculateMultipleRoutesSuccess: getAllTime " + path.getAllTime());

                drawPath(ints[i], path);
            }
        }
    }
}
